package com.kupurui.asteacher.adapter;

import android.content.Context;
import android.text.Html;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.frame.adapter.CommonAdapter;
import com.android.frame.adapter.ViewHolder;
import com.android.frame.ui.BaseActivity;
import com.android.frame.view.linearlistview.LinearListView;
import com.iarcuschin.simpleratingbar.SimpleRatingBar;
import com.kupurui.asteacher.R;
import com.kupurui.asteacher.bean.JobSelInfo;
import com.kupurui.asteacher.utils.URLImageParser;
import com.kupurui.asteacher.utils.UserManger;

import java.util.List;

/**
 * Created by Administrator on 2017/8/10.
 */

public class JobSelAdapter extends CommonAdapter<JobSelInfo> {
    private JobSelRadioAdapter adapter;

    private BaseActivity baseActivity;

    public JobSelAdapter(Context context, List<JobSelInfo> mList, int itemLayoutId) {
        super(context, mList, itemLayoutId);
        this.baseActivity = (BaseActivity) context;
    }

    @Override
    public void convert(ViewHolder holder, final JobSelInfo item, int positon) {
        TextView tvTitle = holder.getView(R.id.tv_title);

        tvTitle.setText(Html.fromHtml(item.getQuestion(), new URLImageParser(mContext,tvTitle), null));

        LinearListView listView = holder.getView(R.id.listview);
        final ImageView ivSelector = holder.getView(R.id.iv_selector);
        SimpleRatingBar ratingBar = holder.getView(R.id.rating_bar);
        ratingBar.setRating(Float.parseFloat(item.getStar()));
        TextView tvSubject = holder.getView(R.id.tv_subject);
        if (item.getType().equals("1")) {
            tvSubject.setText((positon + 1) + ".单选题");
            listView.setVisibility(View.VISIBLE);
            adapter = new JobSelRadioAdapter(mContext, item.getAnswer(), R.layout.radio_item);
            listView.setAdapter(adapter);
        } else if (item.getType().equals("2")) {
            tvSubject.setText((positon + 1) + ".判断题");
            listView.setVisibility(View.VISIBLE);
            adapter = new JobSelRadioAdapter(mContext, item.getAnswer(), R.layout.radio_item);
            listView.setAdapter(adapter);
        } else if (item.getType().equals("3")) {
            tvSubject.setText((positon + 1) + ".填空题");
            listView.setVisibility(View.GONE);
        } else if (item.getType().equals("4")) {
            tvSubject.setText((positon + 1) + ".解答题");
            listView.setVisibility(View.GONE);
        }

        if (item.getIs_selector()) {
            ivSelector.setSelected(true);
        } else {
            ivSelector.setSelected(false);
        }

        ivSelector.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                item.setIs_selector(!item.getIs_selector());
                if (item.getIs_selector()){
                    UserManger.subject_id.add(item.getId());
                    ivSelector.setSelected(true);
                } else{
                    UserManger.subject_id.remove(item.getId());
                    ivSelector.setSelected(false);
                }

                baseActivity.intoActivity(item,0);
            }
        });

    }

    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public boolean isEnabled(int position) {
        return false;
    }
}
